package com.tanguy.user.ui.me.activity.account.join

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.support.v4.content.ContextCompat
import android.support.v4.widget.DrawerLayout
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.Gravity
import android.view.View
import com.tanguy.user.Const
import com.tanguy.user.R
import com.tanguy.user.base.ImmersionTitleActivity
import com.tanguy.user.bean.ProvinceBean
import com.tanguy.user.custom.SuspendDecoration
import com.tanguy.user.custom.WaveSideBar
import com.tanguy.user.interfaces.OnItemClickListener
import com.tanguy.user.net.http.HttpManager
import com.tanguy.user.ui.me.adapter.CityAdapter
import com.tanguy.user.ui.me.adapter.ProvinceAdapter
import com.tanguy.user.utlis.extend.dp2px
import com.tanguy.user.utlis.extend.requestSucceed
import com.tanguy.user.utlis.extend.sp2px
import org.jetbrains.anko.find
import java.util.*

/**
 * Created by dab on 2017/11/3 0003 16:36
 * 选择开通城市
 */
class RegionActivity : ImmersionTitleActivity() {
    override fun setContentViewRes(): Int = R.layout.activity_vehicle_type
    private var mProvinceBeans: List<ProvinceBean> = ArrayList()
    private val mRvBrand by lazy { find<RecyclerView>(R.id.rv_brand) }
    private val mRvType by lazy { find<RecyclerView>(R.id.rv_type) }
    private val mDlRoot by lazy { find<DrawerLayout>(R.id.dl_root) }
    private val mSideBar by lazy { find<WaveSideBar>(R.id.side_bar) }
    private val mProvinceAdapter = ProvinceAdapter()
    private val mCityAdapter = CityAdapter()
    private var brandName = ""
    override fun initView() {
        super.initView()
        title = "选择开通城市"
        titleColor = R.color.textColor
        initRecyclerView()
        mDlRoot.setScrimColor(Color.TRANSPARENT)
        mDlRoot.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
        mDlRoot.addDrawerListener(object : DrawerLayout.SimpleDrawerListener() {
            override fun onDrawerClosed(drawerView: View) {
                super.onDrawerClosed(drawerView)
                mProvinceAdapter.cancelSelect()
            }
        })

        HttpManager.getProvinceList()
                .requestSucceed(this) {
                    if (it == null) return@requestSucceed
                    mProvinceBeans = it
                    Collections.sort(mProvinceBeans)
                    mProvinceAdapter.setProvinceList(mProvinceBeans)
                }

        mSideBar.setOnSelectIndexItemListener { index ->
            for (i in mProvinceBeans.indices) {
                if (mProvinceBeans[i].initial == index) {
                    (mRvBrand.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(i, 0)
                    return@setOnSelectIndexItemListener
                }
            }
        }

    }

    private fun initRecyclerView() {
        val suspendDecoration = object : SuspendDecoration() {
            override fun isSameGroup(priorGroupId: Int, nowGroupId: Int): Boolean {
                return mProvinceBeans[priorGroupId].initial == mProvinceBeans[nowGroupId].initial
            }

            override fun showTitle(position: Int): String {
                return mProvinceBeans[position].initial
            }
        }
        suspendDecoration.setTitleHeight(dp2px(30))
                .setTitleSize(sp2px(14))
                .setTitleGravity(SuspendDecoration.TitleGravity.TITLE_GRAVITY_LEFT)
                .setBackgroundColor(ContextCompat.getColor(this, R.color.color_f5f5f5))
                .setTextColor(ContextCompat.getColor(this, R.color.color_999999))
                .setTextOffsetX(dp2px(16))
        mRvBrand.layoutManager = LinearLayoutManager(this)
        mRvBrand.adapter = mProvinceAdapter
        mRvBrand.addItemDecoration(suspendDecoration)
        //类型
        mRvType.layoutManager = LinearLayoutManager(this)

        mRvType.adapter = mCityAdapter
        mProvinceAdapter.setOnItemClickListener(object : OnItemClickListener {
            override fun onItemClick(view: View, position: Int) {
                val provinceBean = mProvinceBeans[position]
                val code = provinceBean.code
                brandName = provinceBean.name
                getCityList(code)
            }
        })

    }

    private fun getCityList(code: String) {
        HttpManager.getCityList(code)
                .requestSucceed(this) {
                    mCityAdapter.setCityBeans(it)
                    mDlRoot.openDrawer(Gravity.END)
                    mCityAdapter.setDataCallBack({ carTypesBean ->
                        val data = Intent()
                        data.putExtra(Const.IntentCode.CITY_CODE, carTypesBean.cityCode)
                        data.putExtra(Const.IntentCode.CITY_PROVINCE_STRING, brandName + " " + carTypesBean.name)
                        data.putExtra(Const.IntentCode.CITY_STRING,  carTypesBean.name)
                        setResult(Activity.RESULT_OK, data)
                        finish()
                    })
                }
    }

}